/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.bundle;

import com.jspx.json.JSONException;
import com.jspx.scriptmark.util.ScriptConverter;
import com.jspx.txweb.bundle.table.BundleTable;
import com.jspx.txweb.env.TXWeb;
import com.jspx.utils.ObjectUtil;
import com.jspx.utils.SystemUtil;
import com.jspx.utils.StringUtil;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.beans.XMLEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by IntelliJ IDEA.
 * User:chenYuan (mail:39793751@qq.com)
 * Date: 2007-4-27
 * Time: 11:35:25
 */
public abstract class BundleProvider implements Bundle, Serializable
{
    private static final Logger log = LoggerFactory.getLogger(BundleProvider.class);
    protected String namespace = TXWeb.global;
    protected String dataType = StringUtil.empty;
    protected String encode = SystemUtil.encode;
    final protected Map<String, String> cache = new HashMap<String, String>();


    public String getNamespace()
    {
        return namespace;
    }

    public void setNamespace(String namespace)
    {
        this.namespace = namespace;
    }

    public String getDataType()
    {
        return dataType;
    }

    public void setDataType(String dataType)
    {
        this.dataType = dataType;
    }

    public String getEncode()
    {
        return encode;
    }

    public void setEncode(String encode)
    {
        if (StringUtil.isNULL(encode)) return;
        this.encode = encode;
    }

    public String getString(String key)
    {
        return getValue(key);
    }

    public String[] getArray(String key){
        String v = getValue(key);
        return StringUtil.split(StringUtil.replace(v, ",", ";"),";");
    }

    public String get(String key)
    {
        return  get(key,"");
    }

    public String get(String key,String defVar)
    {
        String temp = getValue(key);
        if (StringUtil.isNULL(temp)) return defVar;
        return temp;
    }

    @Override
    public String getString(String key, String defVar) {
        return get( key, defVar);
    }

    public String getLang(String key)
    {
        String temp = getValue(key);
        if (StringUtil.isNULL(temp)) return key;
        return temp;
    }

    public String getLang(String key,String def)
    {
        return getString( key,def);
    }
    public boolean save(String key, String value,int encrypt) throws Exception
    {
        BundleTable bundleTable = new BundleTable();
        bundleTable.setIdx(key);
        bundleTable.setContext(value);
        bundleTable.setDataType(dataType);
        bundleTable.setNamespace(namespace);
        bundleTable.setEncrypt(encrypt);
        return save(bundleTable);
    }

    public boolean getBoolean(String key)
    {
        return StringUtil.toBoolean(get(key));
    }

     public int getInt(String key, int defVar)
    {
        int result = getInt(key);
        return result==0?defVar:result;
    }

    public int getInt(String key)
    {
        return ObjectUtil.toInt(getValue(key));
    }

    public long getLong(String key)
    {
        return ObjectUtil.toLong(getValue(key));
    }

    public double getDouble(String key)
    {
        return ObjectUtil.toDouble(getValue(key));
    }

    public abstract  void flush() throws Exception;

    public abstract List getList();

    public abstract String getValue(String key);

    public abstract void loadMap();

    public abstract boolean remove(String key);

    public int size()
    {
        return cache.size();
    }

    public Map<String,String> toMap()
    {
        loadMap();
        return cache;
    }

    public String toString(String key)
    {
        return getValue(key);
    }

    public String toJson() throws JSONException
    {
        return ScriptConverter.toJson(cache);
    }
    
    public String toXML() throws IOException
    {
        OutputStream sw = new com.jspx.io.StringOutputStream();
        try
        {
            java.beans.XMLEncoder en = new XMLEncoder(sw);
            en.writeObject(getList());
            en.flush();
            en.close();
        } catch (Exception e)
        {
            log.error(e.getLocalizedMessage());
        } finally
        {
            sw.flush();
            sw.close();
        }
        return sw.toString();
    }
}